﻿@{
    var plugName = @admin.cyt369.com.Models.PlugName.Name;
    var tableName = PageViewModel.PageTableID(this, plugName);
    var menu_module_list_id = PageViewModel.ControlID(this, "menu_module_list", plugName);
    var menu_module_list_fmt = PageViewModel.ControlID(this, "menu_module_list_fmt", plugName);
    var menu_module_list_load_success = PageViewModel.ControlID(this, "menu_module_list_load_success", plugName);
    var on_menu_module_selected = PageViewModel.ControlID(this, "on_menu_module_selected", plugName);

    var moduleContextMenu = PageViewModel.ControlID(this, "module_context_menu");
    var onModuleContextMenu = PageViewModel.PageFunctionName(this, "on_module_context_menu");

    var newModuleFun = PageViewModel.PageFunctionName(this, "new_module_fun");
    var editModuleFun = PageViewModel.PageFunctionName(this, "edit_module_fun");
    var editModuleDlgName = PageViewModel.PageEditDialog(this, "edit_module_dlg", "_dlg");
    var saveModuleFunName = PageViewModel.PageFunctionName(this, "save_module_fun");
    var deleteModuleFunName = PageViewModel.PageFunctionName(this, "delete_module_fun");

}
<style type="text/css">
    #layout_@menu_module_list_id{ }
    #layout_@menu_module_list_id .panel-header {
        height:41px;
        background-color:#fdfdfd;
        text-align:center;
    }
</style>

<script type="text/javascript">
    function @menu_module_list_fmt (v, r, i) {
        return "<div style='padding-left:10px;padding-right:10px;height:35px;line-height:35px;cursor:pointer;'>" + r.Name + "</div>";
    }

    function @on_menu_module_selected (index, row) {
        if (row != null) {
            $.ajax({
                url: "~/Systems/GetModelAppMenus/"+row.Id,
                type: "GET",
                success: function (e) {
                    $("#@tableName").treegrid("loadData", e);
                },
                error: function (e) {
                    ShowAjaxError(e);
                }
            });
        }
    }

    function @menu_module_list_load_success (data) {
        if (data.rows.length > 0) {
            $("#@menu_module_list_id").datalist("selectRow", 0);
        }
    }

</script>

<script type="text/javascript">
    function @onModuleContextMenu (e, index, row) {
        e.preventDefault();
        $('#@menu_module_list_id').datalist('selectRow', index);
        $('#@moduleContextMenu').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
    }
    function @editModuleFun () {
        var row = $('#@menu_module_list_id').datalist('getSelected');
        if (row != null) {
            @newModuleFun (row.Id);
        }
    }

    function @newModuleFun (id) {
        var dlg = $dlg('@editModuleDlgName');
        dlg.dialog({
            title: '模块编辑', width: 700, height: 420, closed: false, cache: false, modal: true, maximizable: false, resizable: false, maximized: false,
            href: '~/Systems/_EditAppMenuModule?ID=' + id,
            onResize: function () { $(this).dialog('center'); },
            buttons: [
                {
                    text: "保 存",
                    handler: function () { @saveModuleFunName (); },
                    iconCls: "icon-save"
                }, {
                    text: "取 消",
                    handler: function () { $("#@editModuleDlgName").dialog("close"); },
                    iconCls: "icon-cancel"
                }
            ],
            onLoad: function () {
                LoadObject(id, "~/Systems/GetAppMenuByID/", function (menu) {
                    var form = $("#@editModuleDlgName").find("form");
                    form.form("load", menu);
                    if (menu.Enable) {
                        form.find("#Enable").switchbutton("check");
                    } else {
                        form.find("#Enable").switchbutton("uncheck");
                    }
                });
            }
        });
    }

    function @saveModuleFunName () {
            var form = $("#@editModuleDlgName").find("form");
            if (form.form('enableValidation').form('validate')) {
                var module = form.form_to_json(function (form, module) {
                    module.Enable = form.find("#Enable").switchbutton("options").checked;
                });

                $.ajax({
                    url: "~/Systems/SaveAppMenu",
                    type: "POST",
                    data: module,
                    success: function (e) {
                        if (e.Result) {
                            $.messager.alert('保存成功', '保存成功!', 'info', function () {
                                $('#@editModuleDlgName').dialog('close');
                                $('#@menu_module_list_id').datalist('reload');
                            });
                        } else {
                            $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                        }
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
    }

    function @deleteModuleFunName () {
        var row = $('#@menu_module_list_id').datalist('getSelected');
        if (row != null && confirm("删除该模块后所有菜单将会删除，确认要删除该模块?")) {
            $.ajax({
                async: false,
                method: "DELETE",
                url: $.Content("~/Systems/DeleteAppMenu/" + row.Id),
                context: $("#@menu_module_list_id"),
                success: function (e) {
                    if (e.Result) {
                        this.datalist('reload');
                    } else {
                        $.messager.alert('删除数据错误', '删除失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e1, e2, e3) {
                    ShowAjaxError(e1, "删除数据错误");
                    this.treegrid('reload');
                }
            });
        }
    }
</script>


<ul id="@menu_module_list_id" class="easyui-datalist" title="模块" fit="true" data-options="border:false,lines:true,
                                                                                  url: '~/Systems/GetAppModels',method:'get',
                                                                                  textField:'Name',valueField:'Id',selectOnCheck:false,
                                                                                  textFormatter:@menu_module_list_fmt,
                                                                                  onSelect:@on_menu_module_selected,
                                                                                  onLoadSuccess:@menu_module_list_load_success,
                                                                                  onRowContextMenu:@onModuleContextMenu"></ul>
<div id="@moduleContextMenu" class="easyui-menu" style="width:120px;">
    <div onclick="@newModuleFun ()" data-options="iconCls:'icon-add'">新增</div>
    <div onclick="@editModuleFun ()" data-options="iconCls:'icon-edit'">修改</div>
    <div onclick="@deleteModuleFunName ()" data-options="iconCls:'icon-remove'">删除</div>
</div>
